import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:flutter_base_plugin/flutter_base_plugin.dart';
import 'package:flutter_base_plugin_example/config/app_config_impl.dart';
import 'package:flutter_base_plugin_example/config/load_config_impl.dart';
import 'package:flutter_base_plugin_example/net/http_bean_impl.dart';
import 'package:flutter_base_plugin_example/page/index_page.dart';
import 'package:flutter_base_plugin_example/page/login/login_page.dart';
import 'package:flutter_base_plugin_example/page/main/main_page.dart';
import 'package:flutter_base_plugin_example/providers/provider_set.dart';
import 'package:flutter_base_plugin_example/widgets/init_load_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'app_manager/app_router_manager.dart';
import 'config/app_config.dart';
import 'config/k_color.dart';
import 'net/http_config_impl.dart';

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    super.initState();
    _initBasePlugin();
  }

  void _initBasePlugin() async {
    await BasePlugin.init(
      isDebug: true,
      appConfig: AppConfigImpl(),
      httpConfig: HttpConfigImpl(),
      httpBean: HttpBeanImpl(),
      loadConfig: LoadConfigImpl(),
      //bugly相关配置
      logConfig: LogConfig.obtain(
        tag: "JoeKaiy",
        isDebug: true,
        //保存到外部路径时，需要获取存储权限
        saveLogFilePath: '/storage/emulated/0/flutter_xlog_app/baselib_xlog/',
        encryptPubKey: null,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    AppRoutesManager.initRouter();
    //预先加载SPUtil
    return FutureBuilder<SpUtils?>(
      future: SpUtils.perInit(),
      builder: (BuildContext context, AsyncSnapshot<SpUtils?> snapshot) {
        //加载完成进入HomePgae 否则就转圈
        Widget widget = snapshot.connectionState == ConnectionState.done
            ? _buildWidget() //自己定义的页面
            : InitLoadWidget();
        return widget;
      },
    );
  }

  _buildWidget() {
    return ScreenUtilInit(
      designSize: const Size(750, 1334),
      builder: (c, child) {
        return MultiProvider(
          providers: providers,
          child: RefreshConfiguration(
              // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
              headerBuilder: () => const ClassicHeader(
                    releaseText: "松开开始刷新数据",
                    completeText: "刷新成功！",
                    refreshingText: "数据刷新中···",
                    failedText: "刷新失败，请重试！",
                    idleText: "下拉刷新",
                  ),
              // 配置默认底部指示器
              footerBuilder: () => const ClassicFooter(
                    loadingText: "加载中…",
                    idleText: "上拉加载",
                    failedText: "加载失败",
                    canLoadingText: "松手开始加载数据",
                    noDataText: "暂无更多数据",
                  ),
              //自定义回弹动画,三个属性值意义请查询flutter api
              springDescription: const SpringDescription(
                  stiffness: 170, damping: 16, mass: 1.9),
              maxOverScrollExtent: 100,
              //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
              footerTriggerDistance: -60,
              // 底部触发刷新的越界距离
              maxUnderScrollExtent: 100,
              // 底部最大可以拖动的范围
              enableScrollWhenRefreshCompleted: true,
              //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
              enableLoadingWhenFailed: true,
              //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
              hideFooterWhenNotFull: false,
              // Viewport不满一屏时,禁用上拉加载更多功能
              enableBallisticLoad: true,
              // 可以通过惯性滑动触发加载更多
              child: MaterialApp(
                title: AppConfig.appName,
                debugShowCheckedModeBanner: false,
                onGenerateRoute: AppRoutesManager.router.generator,
                //初始化全局context
                builder: OneContextUtils.oneContextBuilder,
                navigatorKey: OneContextUtils.oneContextKey,
                //定制主题
                theme: ThemeData(primaryColor: KColors.primaryColor),
                // home: LoginPage(),
                home: IndexPage(),
              )),
        );
      },
    );
  }
}
